package com.app;


import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.GestureDetector;

import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

import android.view.View;
import android.view.View.OnTouchListener;


public class GraphView extends View implements OnTouchListener {
	Canvas canvas;
	public Paint paint = new Paint();
	private Point point;
	private int odvid = 20;
	private int minvid = 20;
	ScaleGestureDetector sd;
	GestureDetector gl;
	private Point currentFingerCoor = new Point(10, 10);
	private short fingerflag = 0;
	private Graph graph;
	private GraphicsActivity activity;

	// constructors
	public GraphView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public GraphView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public GraphView(Context context) {
		super(context);
		init(context);
	}

	// methods
	public void init(Context context) {
		this.setBackgroundColor(Color.WHITE);
		this.setOnTouchListener(this);

		setFocusable(true);
		setFocusableInTouchMode(true);
		paint.setColor(Color.BLACK);
		sd = new ScaleGestureDetector(context, new ScaleListener());
		gl = new GestureDetector(context, new GListener());
		

	}

	// call after invalidate()
	public void onDraw(Canvas canvas) {
		
		
		if (point != null && graph!=null) {
			graph.drawPlane((int) point.x, (int) point.y,odvid);
		} else {
			point = new Point(canvas.getWidth() / 2, canvas.getHeight() / 2);
			graph=new Graph(canvas,paint);
			graph.drawPlane((int) point.x, (int) point.y,odvid);
		}
		
		if (fingerflag == 1)
			graph.drawFingerCoordinates(currentFingerCoor,point,odvid,activity);

	}

	public boolean onTouch(View arg0, MotionEvent event) {

		sd.onTouchEvent(event);
		gl.onTouchEvent(event);

		invalidate();
		return true;
	}





	class ScaleListener extends
			ScaleGestureDetector.SimpleOnScaleGestureListener {
		float riznx;
		float rizny;
		float odvidx;
		float odvidy;
		
		public boolean onScale(ScaleGestureDetector detector) {
			fingerflag = 0;
			Point focus = new Point(detector.getFocusX(), detector.getFocusY());// current
																				// point
			riznx = point.x - focus.x;
			rizny = point.y - focus.y;
			odvidx = riznx / odvid;
			odvidy = rizny / odvid;

			if (odvid > minvid
					|| ((detector.getPreviousSpan() - detector.getCurrentSpan()) / 3) < 0) {
				odvid -= (int) ((detector.getPreviousSpan() - detector
						.getCurrentSpan()) / 3);
				point.x = focus.x + (odvidx * odvid);
				point.y = focus.y + (odvidy * odvid);
				invalidate();

			}
			return true;
		}
	}
	
	
	class GListener implements GestureDetector.OnGestureListener {

		@Override
		public boolean onDown(MotionEvent e1) {
			currentFingerCoor.x = e1.getX();
			currentFingerCoor.y = e1.getY();
			fingerflag = 1;
			return true;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float arg2,
				float arg3) {
			return true;
		}

		@Override
		public void onLongPress(MotionEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float arg2,
				float arg3) {
			fingerflag = 0;
			point.x -= arg2;
			point.y -= arg3;
			invalidate();
			return true;
		}

		@Override
		public void onShowPress(MotionEvent arg0) {

		}

		@Override
		public boolean onSingleTapUp(MotionEvent arg0) {

			return false;
		}

	}

	// Setter
	public void setFunction(String[] functions) {
		graph.setFunctions(functions);
		invalidate();
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity =(GraphicsActivity)activity;
	}

}
